package is.glif.fenius.viz.invoke;

import static java.util.Arrays.asList;

import javax.servlet.Servlet;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import is.glif.fenius.viz.http.FeniusList;
import is.glif.fenius.viz.http.JettyLauncher;

public class Invoker {
    public static void main(String[] args) throws Exception {

        // create a parser
        OptionParser parser = new OptionParser() {
            {
                acceptsAll( asList( "h", "?" ), "show help then exit" );
                accepts( "help", "show extended help then exit" );
                accepts( "port" , "HTTP port" ).withRequiredArg().describedAs("int (8080 default)").ofType(Integer.class);
                accepts( "host" , "host" ).withRequiredArg().describedAs("host (0.0.0.0 default)").ofType(String.class);
            }
        };

        OptionSet options = parser.parse( args );

        // check for help
        if ( options.has( "?" ) || options.has("h")) {
            parser.printHelpOn( System.out );
            System.exit(0);
        }
        Integer port = 8080;
        String host = "0.0.0.0";
        JettyLauncher launcher = JettyLauncher.getInstance();
        if (options.has("port")) {
            port = (Integer) options.valueOf("port");
        }
        if (options.has("host")) {
            host = (String) options.valueOf("host");
        }
        Servlet servlet = new FeniusList();
        launcher.initServer(port, host, servlet);
        launcher.getServer().start();
        while (true) {
            Thread.sleep(100);
        }

    }

}
